/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.info.core;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 类LinuxUpload.java的实现描述： 远程传输文件类实现
 * 
 * @author huangbin 2010-6-17 下午02:45:28
 */
public class LinuxUpload {

    private static Log logger = LogFactory.getLog(LinuxUpload.class);

    public static void scpWrite(InfoFileConfigure info, String localFile, String remoteFile, boolean html) {
        String[] hostArr = html ? info.getRemoteHtmlHosts() : info.getRemoteFileHosts();
        String user = html ? info.getRemoteHtmlUser() : info.getRemoteFileUser();
        for (String host : hostArr) {
            String command = "scp -r " + localFile + " " + user + "@" + host + ":" + remoteFile;
            if (logger.isDebugEnabled()) {
                logger.debug(command); // 整个命令日志
                // logger.debug("scp [" + localFile + "] to " + info.getRemoteHost() + info.getRemotePath());
            }
            Process p = null;
            try {
                Runtime rt = Runtime.getRuntime();
                p = rt.exec(command);
                if (p.waitFor() != 0) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("exec scp return: exit value-> " + p.exitValue());
                    }
                    if (logger.isInfoEnabled()) {
                        logger.info("java call scp command is error");
                    }
                }
            } catch (IOException e) {
                if (logger.isErrorEnabled()) {
                    logger.error(e);
                }
            } catch (InterruptedException e) {
                if (logger.isErrorEnabled()) {
                    logger.error(e);
                }
            }
        }
    }

    public static void scpMkdir(InfoFileConfigure info, String dir, boolean html) {
        String remote = html ? info.getRemoteHtmlPath() : info.getRemoteFilePath();
        String[] hostArr = html ? info.getRemoteHtmlHosts() : info.getRemoteFileHosts();
        String user = html ? info.getRemoteHtmlUser() : info.getRemoteFileUser();
        if (remote.length() > 0) {
            dir = remote + "/" + dir;
            for (String host : hostArr) {
                String command = "ssh " + user + "@" + host + " mkdir -p " + dir;
                if (logger.isDebugEnabled()) {
                    logger.debug(command); // 整个命令日志
                }
                Process p = null;
                try {
                    Runtime rt = Runtime.getRuntime();
                    p = rt.exec(command);
                    if (p.waitFor() != 0) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("exec ssh return: exit value-> " + p.exitValue());
                        }
                        if (logger.isInfoEnabled()) {
                            logger.info("java call ssh command is error:" + dir);
                        }
                    }
                } catch (IOException e) {
                    if (logger.isErrorEnabled()) {
                        logger.error(e);
                    }
                } catch (InterruptedException e) {
                    if (logger.isErrorEnabled()) {
                        logger.error(e);
                    }
                }
            }
        }
    }

    public static void scpWriteFile(InfoFileConfigure info, String localFile, boolean html) {
        String remote = html ? info.getRemoteHtmlPath() : info.getRemoteFilePath();
        // if (info.getHtmlBasePath().length() > 0) {
        // remote = remote + "/" + info.getHtmlBasePath();
        // }
        scpWrite(info, localFile, remote, html);
    }

    public static void rcpWrite(InfoFileConfigure info, String localFile) {
        // String command = "rcp -r" + localFile + " " + info.getRemoteHost() + ":" + info.getRemotePath();
        // if (logger.isDebugEnabled()) {
        // logger.debug(command); // 整个命令日志
        // // logger.debug("rcp [" + localFile + "] to " + info.getRemoteHost() + info.getRemotePath());
        // }
        // Process p = null;
        // try {
        // Runtime rt = Runtime.getRuntime();
        // p = rt.exec(command);
        // if (logger.isDebugEnabled()) {
        // logger.debug("exec rcp return " + p);
        // }
        // } catch (IOException e) {
        // if (logger.isErrorEnabled()) {
        // logger.error(e);
        // }
        // }

    }

    public static void exec(String command) {
        if (command != null && command.length() > 0) {
            if (logger.isDebugEnabled()) {
                logger.debug(command); // 整个命令日志
            }
            Process p = null;
            try {
                Runtime rt = Runtime.getRuntime();
                p = rt.exec(command);
                if (p.waitFor() != 0) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("exec command: exit value-> " + p.exitValue());
                    }
                    if (logger.isInfoEnabled()) {
                        logger.info("java call command is error!");
                    }
                }
            } catch (IOException e) {
                if (logger.isErrorEnabled()) {
                    logger.error(e);
                }
            } catch (InterruptedException e) {
                if (logger.isErrorEnabled()) {
                    logger.error(e);
                }
            }
        }
    }

    public static void exec(String[] command) {
        if (command != null && command.length > 0) {
            String temp = "";
            for (String s : command) {
                temp = temp + s + " ";
            }
            if (logger.isDebugEnabled()) {
                logger.debug(temp); // 整个命令日志
            }
            Process p = null;
            try {
                Runtime rt = Runtime.getRuntime();
                p = rt.exec(command);
                if (p.waitFor() != 0) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("exec command: exit value-> " + p.exitValue());
                    }
                    if (logger.isInfoEnabled()) {
                        logger.info("java call command is error!");
                    }
                }
            } catch (IOException e) {
                if (logger.isErrorEnabled()) {
                    logger.error(e);
                }
            } catch (InterruptedException e) {
                if (logger.isErrorEnabled()) {
                    logger.error(e);
                }
            }
        }
    }

}
